package com.groot.medical.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.ChronicDisease;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.service.ChronicService;
import com.groot.medical.service.ChronicServiceImpl;

@WebServlet("/sys/illtype.do")
public class ChronicController extends BaseServletController {
	
	private static final long serialVersionUID = 1L;
	private ChronicService chroniceService = new ChronicServiceImpl();
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}

	/**
	 * 分页查询慢性病类型
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findIllTypeByPge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 5;
		PageBean areaPage = chroniceService.findByPage(pageNum, pageSize);
		System.out.println("areaPage:"+areaPage);
		request.setAttribute("areaPage", areaPage);
		
		return "/WEB-INF/page/illtype/illtype_list.jsp";
		
	}
	/**
	 * 打开添加慢性病信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openAddIllInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		return "/WEB-INF/page/illtype/illtype_add.jsp";
	}
	/**
	 * 添加慢性病类型信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String AddIllInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String tgId = request.getParameter("tgId");
		String illCode = request.getParameter("illCode");
		String illName = request.getParameter("illName");
		String illWbm = request.getParameter("illWbm");
		String illPym = request.getParameter("illPym");
		ChronicDisease ill = new ChronicDisease();
		ill.setIllCode(illCode);
		ill.setIllName(illName);
		ill.setPyCode(illPym);
		ill.setWbCode(illWbm);
		chroniceService.AddIllInfo(ill);
		request.setAttribute("tgId", tgId);
		
		
		return findIllTypeByPge(request,response);
	}
	/**
	 * 打开慢性病分类修改页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openEditIllTypePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		//查询待修改数据信息
		ChronicDisease ill=chroniceService.findIllTypeById(id);
		request.setAttribute("tgId",request.getParameter("tgId"));
		request.setAttribute("ill",ill);
		return "/WEB-INF/page/illtype/illtype_edit.jsp";
	}
	/**
	 * 修改慢性病类型
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editIllInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String id = request.getParameter("id");
		String illCode = request.getParameter("illCode");
		String illName = request.getParameter("illName");
		String illWbm = request.getParameter("illWbm");
		String illPym = request.getParameter("illPym");
		System.out.println("大大大"+id);
		ChronicDisease ill = new ChronicDisease();
		ill.setId(Integer.parseInt(id));
		ill.setIllCode(illCode);
		ill.setIllName(illName);
		ill.setPyCode(illPym);
		ill.setWbCode(illWbm);
		//执行修改方法
		chroniceService.editIllInfo(ill);
		request.setAttribute("tgId", tgId);
		return findIllTypeByPge(request,response);
	}
	/**
	 * 删除慢性病类型
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteIllTypePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String tgId = request.getParameter("tgId");
		String id = request.getParameter("id");
		System.out.println("大大大"+id);
		int flag=chroniceService.deleteIllTypePage(id);
		String resultString = "true";
       
		response.setContentType("text/html");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(resultString.getBytes());
        outputStream.close();

		
	}
}
